package com.blogSystem.article.service;

import com.blogSystem.article.dao.ArticleDao;
import com.blogSystem.article.dao.ThumbsMapper;
import com.blogSystem.article.pojo.Article;
import com.blogSystem.article.pojo.Thumbs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.List;

@Service
@Transactional
public class ThumbsSerivlce {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ThumbsMapper thumbsMapper;
    @Autowired
    private ArticleDao articleDao;

    public void addThumbs(Thumbs thumbs){
        thumbs.setId(idWorker.nextId()+"");
        thumbsMapper.insert(thumbs);
        Article article=articleDao.findById(thumbs.getArticle_id()).get();
        article.setThumbs(article.getThumbs()+1);
        articleDao.save(article);
    }
    public void deleteThumbs(String article_id,String user_id){

        Thumbs thumbs=thumbsMapper.selectByUserIdAndArticleId(user_id,article_id);
        if(thumbs!=null) {
            thumbsMapper.deleteById(thumbs.getId());
            Article article = articleDao.findById(thumbs.getArticle_id()).get();
            article.setThumbs(article.getThumbs() - 1);
            articleDao.save(article);
        }
    }
    public Thumbs findThumbs(String article_id,String user_id){
        Thumbs thumbs=thumbsMapper.selectByUserIdAndArticleId(user_id,article_id);
        return thumbs;
    }
    public List<Thumbs> findUserThumbs(String user_id){
        return thumbsMapper.selectByUserId(user_id);
    }
    public List<Thumbs> findArticleThumbs(String article_id){

        return thumbsMapper.selectByArticleId(article_id);
    }

    public void deleteByarticle(String article_id){

        thumbsMapper.deleteByArticleId(article_id);
    }

}
